package com.polimi.meteocal.security;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.LazyScheduleModel;
import org.primefaces.model.ScheduleModel;

import com.polimi.meteocal.business.control.CalendarManager;
import com.polimi.meteocal.business.entity.Event;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@ManagedBean
@SessionScoped
public class SearchCalendarBean implements Serializable {
/*
 * This Bean searches the calendar request by the User and checks if it is public
 */
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@EJB
	CalendarManager cm;
	
	private String calendarToSearch;
	private List<Event> events;
	
	private String result="MyCalendar";
	

	public void searchCalendar(){
		
		
		FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        ServletContext sContext = request.getSession().getServletContext();
        sContext.setAttribute("calendarToSearch", calendarToSearch);
		//Calendar Check-MISSING
		if(cm.getCalendarPrivacy(calendarToSearch)){
			
			this.result=calendarToSearch+" Calendar";
			
			//Calendar Loading
			events= cm.getUserEvents(calendarToSearch);
			
	        sContext.setAttribute("userEvents", events);
	        sContext.setAttribute("otherCalendar", true);
		
		}
		
		else{
			 sContext.setAttribute("otherCalendar", false);
		}
		
		
	}
	

	public String getCalendarToSearch() {
		return calendarToSearch;
	}


	public void setCalendarToSearch(String calendarToSearch) {
		this.calendarToSearch = calendarToSearch;
	}
	

	public List<Event> getEvents() {
		return events;
	}


	public void setEvents(List<Event> events) {
		this.events = events;
	}


	public String getResult() {
		return result;
	}


	public void setResult(String result) {
		this.result = result;
	}
}
